# -*- coding:utf-8 -*-
import os
import math
from collections import defaultdict


class Overlap(object):
    def __init__(self):
        pass

    @staticmethod
    def score(query: list, candidate: list):
        d_sent1, d_sent2 = defaultdict(int), defaultdict(int)

        for word in query:
            d_sent1[word] += 1

        for word in candidate:
            d_sent2[word] += 1

        n_shared_word_in_sent1 = sum([d_sent1[w] for w in d_sent1 if w in d_sent2])
        n_shared_word_in_sent2 = sum([d_sent2[w] for w in d_sent2 if w in d_sent1])
        n_tol = sum(d_sent1.values()) + sum(d_sent2.values())
        if 1e-6 > n_tol:
            return 0.0
        else:
            return 1.0 * (n_shared_word_in_sent1 + n_shared_word_in_sent2) / n_tol


class TFIDFOverlap(object):
    def __init__(self, idf_dict_path):
        self._idf_dict = self.load_idf_dict(idf_dict_path)
        pass

    @staticmethod
    def load_idf_dict(path):
        idf_dict = {}
        with open(path, 'r', encoding='utf-8') as fr:
            for line in fr:
                ss = line.strip().split('\t')
                word = ss[0]
                idf = float(ss[1])
                idf_dict[word] = idf
        return idf_dict

    def score(self, query: list, candidate: list):
        sent1_dict = {}
        sent2_dict = {}

        for word in query:
            sent1_dict[word] = sent1_dict.get(word, 0) + 1
        for word in candidate:
            sent2_dict[word] = sent2_dict.get(word, 0) + 1

        sum_shared_word_in_sent1 = sum(
            [sent1_dict[w] * self._idf_dict.get(w, 0) for w in sent1_dict if w in sent2_dict])
        sum_shared_word_in_sent2 = sum(
            [sent2_dict[w] * self._idf_dict.get(w, 0) for w in sent2_dict if w in sent1_dict])

        sum_tol = sum(sent1_dict[w] * self._idf_dict.get(w, 0) for w in sent1_dict)
        sum_tol += sum(sent2_dict[w] * self._idf_dict.get(w, 0) for w in sent2_dict)

        if 1e-6 > sum_tol:
            return [0.]
        else:
            return [1.0 * (sum_shared_word_in_sent1 + sum_shared_word_in_sent2) / sum_tol]
        pass


class NEROverlap(object):
    def __init__(self):
        pass

    @staticmethod
    def match(query_ner_words, candidate_ner_words):
        query_ner_set = set(query_ner_words)
        candidate_ner_set = set(candidate_ner_words)

        score = len(query_ner_set & candidate_ner_set) * 1.0 / len(query_ner_set | candidate_ner_set)
        return score
        pass
